package com.sleepcamel.resteasyConfigurator.view.sourcecode;

import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.CompositeRuler;
import org.eclipse.jface.text.source.LineNumberRulerColumn;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

import com.sleepcamel.rest.domain.ISourceCodeGenerable;
import com.sleepcamel.rest.domain.sourcegenerator.ISourceCodeGenerator;
import com.sleepcamel.rest.domain.sourcegenerator.SourceCodeGeneratorProvider;

public class SourceCodeView extends Composite {

	private IDocument document;
	private SourceViewer viewer;
	ISourceCodeGenerateStrategy sourceCodeGenerateStrategy;

	public SourceCodeView(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout());

		CompositeRuler ruler = new CompositeRuler(12);
		viewer = new SourceViewer(this, ruler, SWT.V_SCROLL | SWT.H_SCROLL
				| SWT.WRAP | SWT.MULTI);
		viewer.setEditable(true);
		ruler.addDecorator(0, new LineNumberRulerColumn());
//		viewer = new JavaSourceViewer(this, ruler, null, false, SWT.V_SCROLL | SWT.H_SCROLL
//				| SWT.WRAP | SWT.MULTI, null);
//		SimpleJavaSourceViewerConfiguration simpleJavaSourceViewerConfiguration = new SimpleJavaSourceViewerConfiguration(JavaPlugin.getDefault().getJavaTextTools().getColorManager(), null, null, "___java_partitioning", false);
//		viewer.configure(simpleJavaSourceViewerConfiguration);
//		viewer.configure(new JavaSourceViewerConfiguration());
	}

	public void refresh() {
		SourceCodeGeneratorProvider sourceCodeGeneratorProvider = SourceCodeGeneratorProvider.getInstance();
		ISourceCodeGenerator sourceGenerator = sourceCodeGeneratorProvider.getGeneratorFor(getData());
		if ( sourceGenerator != null && getData() instanceof ISourceCodeGenerable){
			document = new Document();
			sourceGenerator.setData((ISourceCodeGenerable) getData());
			document.set(sourceCodeGenerateStrategy.generate(sourceGenerator));
			viewer.setDocument(document);
		}
	}

	public void setSourceCodeGenerateStrategy(ISourceCodeGenerateStrategy sourceCodeGenerateStrategy) {
		this.sourceCodeGenerateStrategy = sourceCodeGenerateStrategy;
	}
}
